Kernel.module.define('statusbar', {
	
///////////////////////////////////////////////////////////////
// Define widget module properties here
///////////////////////////////////////////////////////////////

	title: 'statusbar',
	//file: 'statusbar.php',
	widget: true,

	
///////////////////////////////////////////////////////////////
// Define widget global objects & properties here
///////////////////////////////////////////////////////////////

	// init() is the constructor
	init: function() {
		var module = this;
		var gpsIconStage = 'init';
		var initCompass = false;
		var oldCourse = '';
		var oldAccuracy = '';
		var oldAlt = '';
		var oldSpeed = '';
		var oldTime = '';
		var imageSatellite = new Image();
		var imageCompassBG = new Image();
		var imageCompassArrow = new Image();
		var x_pos = window.innerWidth;

        var stage = new Kinetic.Stage({
          container: module.renderTo,
          width: x_pos,
          height: 50		  
        });
        var layer = new Kinetic.Layer();
        var statusbarLayer = new Kinetic.Layer();

		var timeText = new Kinetic.Text({
			x: x_pos-250,
			y: 5,
			fontSize: '40',
			fontFamily: 'Helvetica',
			fill: 'white'
		}); 
		var homeText = new Kinetic.Text({
			x: 10,
			y: 5,
			fontSize: '40',
			fontFamily: 'Helvetica',
			fill: '#aaa'
		}); 

		var cross1 = new Kinetic.Line({
		  x: x_pos-40,
		  y: 33,
		  points: [4, 4, 10, 10, 12, 12],
		  stroke: 'red',
		  tension: 1
		});
		var cross2 = new Kinetic.Line({
		  x: x_pos-24,
		  y: 33,
		  points: [-4, 4, -10, 10, -12, 12],
		  stroke: 'red',
		  tension: 1
		});
		var satCurv1 = new Kinetic.Arc({
			x: x_pos-42,
			y: 32,
			innerRadius: 3,
			outerRadius: 6,
			fill: '#009900',
			stroke: '#000000',
			strokeWidth: 0.3,
			angle: 160,
			width: 2,
			rotationDeg: 55
		});
		var satCurv2 = new Kinetic.Arc({
			x: x_pos-44,
			y: 33,
			innerRadius: 5,
			outerRadius: 8,
			fill: '#00bb00',
			stroke: '#000000',
			strokeWidth: 0.3,
			angle: 160,
			width: 2,
			rotationDeg: 60
		});
		var satCurv3 = new Kinetic.Arc({
			x: x_pos-46,
			y: 35,
			innerRadius: 7,
			outerRadius: 10,
			fill: '#00dd00',
			stroke: '#000000',
			strokeWidth: 0.3,
			angle: 160,
			rotationDeg: 50
		});
		
		imageSatellite.onload = function() {
			satellite = new Kinetic.Image({
			  x: x_pos-50,
			  y: 5,
			  image: imageSatellite
			});
			statusbarLayer.add(satellite);
			statusbarLayer.draw();
		};
		imageSatellite.src = 'widgets/statusbar/images/gps-icon.png';

		imageCompassBG.onload = function() {
			compassBG = new Kinetic.Image({
				image: imageCompassBG,
				x: x_pos-90,
				y: 25,
				offset: {x:25,y:25},
				width: 50,
				height: 50
			});
//			statusbarLayer.add(compass);
//			statusbarLayer.draw();
        };
		imageCompassBG.src = 'widgets/statusbar/images/compass-icon-bg.png';

		imageCompassArrow.onload = function() {
			compassArrow = new Kinetic.Image({
				image: imageCompassArrow,
				x: x_pos-90,
				y: 25,
				offset: {x:5,y:13},
				width: 11,
				height: 26
			});
        };
		imageCompassArrow.src = 'widgets/statusbar/images/compass-icon-arrow.png';


		module.sandbox.vehiclePositioningStart();
		homeText.setText("lll");
		timeText.setText("");
		statusbarLayer.add(timeText);
		statusbarLayer.add(homeText);
		stage.add(layer);
		stage.add(statusbarLayer);
		layer.draw();

		//$('body').append("<div id='errorLayer' style='display:none; position: absolute; top: 50px; left: 200px; width: 400px; height: 50px; color: #fff; background-color: #ee2222; z-index: 100;-webkit-transform: translate3d(0, 0, 0); text-align:center;font-size:200%;'></div>");

///////////////////////////////////////////////////////////////
// Define event listeners here
///////////////////////////////////////////////////////////////

		homeText.on('mouseup', function(e) {
			$(module.sandbox.mRunningFSA).slideToggle(300);
//			$('#launcherContentLyr').slideToggle(300);
//			module.removeHomeIcon(statusbarLayer, homeText);
		});

		module.sandbox.listen('FSA', function(data) {
			if (data) {
				module.addHomeIcon(statusbarLayer, homeText);
			}
		}),

		module.sandbox.listen('gps-time', function(data) {
			if (data !== "") {
				if (gpsIconStage == 'nofix') {
					statusbarLayer.remove(cross1, cross2);
					gpsIconStage = "";
				}
				timeText.remove();
				timeText.setText(data);
				statusbarLayer.add(timeText); 
				
				if (!initCompass) {
					statusbarLayer.add(compassBG);
					statusbarLayer.add(compassArrow);
					initCompass = true;
				}
				statusbarLayer.draw();
				oldTime = data;
			}
		});

		module.sandbox.listen('gps-position', function(data) {
			if (data !== "") {
 			}
		});

		module.sandbox.listen('gps-altitude', function(data) {
			if (data !== "") {
				if (oldAlt !== "") altText.remove();

				altText = new Kinetic.Text({
					x: 140,
					y: 5,
					fontSize: '40',
					fontFamily: 'Helvetica',
					fill: 'white'
				}); 

				altText.setText(data);
//				statusbarLayer.add(altText);
//				layer.drawScene();
				oldAlt = data;
 			}
		});

		module.sandbox.listen('gps-speed', function(data) {
			if (data !== "") {
				if (oldSpeed !== "") speedText.remove();

				speedText = new Kinetic.Text({
					x: 280,
					y: 5,
					fontSize: '40',
					fontFamily: 'Helvetica',
					fill: 'white'
				}); 

				speedText.setText(data);
	//			statusbarLayer.add(speedText);
	//			layer.drawScene();
				oldSpeed = data;
 			}
		});

		module.sandbox.listen('gps-course', function(data) {			
			if (data !== "" && oldCourse !== data) {
				if (oldCourse !== '') {
					reset = 360 - oldCourse;
					compassArrow.rotate(reset);
				}
				compassArrow.rotate(data);
				oldCourse = data;
			}
		});

		module.sandbox.listen('gps-accuracy', function(data) {
			if (data !== "" && oldAccuracy !== data) {
				satCurv3.remove();
				satCurv2.remove();
				satCurv1.remove();
				switch (data) {
					case "vg":
						statusbarLayer.add(satCurv3);
					case "g":
						statusbarLayer.add(satCurv2);
					case "a":
						statusbarLayer.add(satCurv1);
//						statusbarLayer.batchDraw();
						break;
						
					default:
				}
				oldAccuracy = data;
			}
		});
		
		module.sandbox.listen('gps-nofix', function(data) {
			if (data !== ""  && gpsIconStage !== "nofix") {
				// cant remove because vg/g/a variable
				satCurv3.remove();
				satCurv2.remove();
				satCurv1.remove();
				statusbarLayer.add(cross1, cross2);
				statusbarLayer.draw();
				gpsIconStage = "nofix";
			} else if (data !== "nofix") {
				cross1.remove();
				cross2.remove();
				statusbarLayer.draw();
				gpsIconStage = "";
			}
		});

		module.sandbox.listen('gps-error', function(data) {
			if (data !== "") {
				alert('gpserrror');
			}
		});

		module.sandbox.listen('SYSTEM-ERROR', function(data) {
			if (data !== "") {
				$("#errorLayer").html(data);
				alertify.error(data);
				// do self heal here
			}
		});

	},
	
	addHomeIcon: function(statusbarLayer, homeText) {
		var module = this;
		statusbarLayer.add(homeText);
		statusbarLayer.draw();
	},
	
	removeHomeIcon: function(statusbarLayer, homeText) {
		var module = this;
		statusbarLayer.remove(homeText);
	},
		
	kill: function() {
		var module = this;
		module.sandbox.vehiclePositionStop();
		layer.destroy();
		statusbarLayer.destroy();
	}
});
